<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CarPackage;
use App\Admin\Services\PackageService;
use App\Models\Brand;
use App\Models\CarPackageCategory;
use App\Models\CarPackageType;
use App\Models\CarPackage as CarPackageModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CarPackageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(CarPackage::with(['brand', 'category']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('brand.title', admin_trans('car-package.fields.brand_id'));
            $grid->column('category.title', admin_trans('car-package.fields.category_id'));
            $grid->column('title');
            $grid->column('status')->using(admin_trans('car-package.options.status'));
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('title');
                $brand = Brand::all()->pluck('title', 'id');
                $filter->equal('brand_id')->select($brand);
                $filter->equal('type')->select(admin_trans('car-package.options.type'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, CarPackage::with(['brand', 'category']), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('brand.title',  admin_trans('car-package.fields.brand_id'));
            $show->field('category.title', admin_trans('car-package.fields.category_id'));
            $show->field('status')->using(admin_trans('car-package.options.status'));
            $show->field('attrs')->unescape()->as(function ($attrs) {
                if ($attrs) {
                    $attrs = json_decode($attrs, true);

                    $html = '';
                    foreach ($attrs as $key => $value) {
                        $type = CarPackageType::find($value['type_id']);
                        $html .= "<span>{$type->title} - {$value['content']} - {$value['price']}元 - {$value['count']}次</span><br /><br />";
                    }
                    return $html;
                }
            });

            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $category_options = CarPackageCategory::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id');

        return Form::make(new CarPackage(), function (Form $form) use ($category_options) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('brand_id')
                ->options(Brand::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->load('category_id', 'package-has-category')
                ->required();
            $form->select('category_id')
                ->options($category_options)
                ->required();
            $form->radio('status')->options(admin_trans('car-package.options.status'))->default('1');
            $form->table('attrs', '续保券', function ($table) {
                $table->text('content', '内容')->required();
                $table->text('price', '价格')->required();
                $table->text('count', '使用次数')->required();
                $table->hidden('sort');
            })->saveAsJson();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function hasCategory(Request $request)
    {
        $brand_id = $request->get('q');

        return CarPackageCategory::where('brand_id', $brand_id)->get(['id', DB::raw('title as text')]);
    }

    public function mergePackage(Request $request)
    {
        $category_id = $request->get('q');

        $package_options = CarPackageModel::where('category_id', $category_id)
            ->get(['id', DB::raw('title as text')]);

        return $package_options;
    }
}
